package org.yxk.mongodb;

import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.annotation.Resource;

import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Repository;

@Repository("userDaoImpl")
public class UserDaoImpl implements UserDao {

	@Resource
	private MongoTemplate mongoTemplate;

	@Override
	public void insert(User object, String collectionName) {
		mongoTemplate.insert(object, collectionName);
	}

	@Override
	public User findOne(Map<String, Object> params, String collectionName) {
		
		return mongoTemplate.findOne(addParams(params), User.class, collectionName);
	}
	private Query addParams(Map<String, Object> params){
		Criteria c = new Criteria();
		if (params != null && params.size() > 0) {
			Criteria cs[] = new Criteria[params.size()];
			int n = 0;
			for (Entry<String, Object> p : params.entrySet()) {
				cs[n]=Criteria.where(p.getKey()).is(p.getValue());
				n++;
			}
			c.andOperator(cs);
		}
		return new Query(c);
	}
	@Override
	public List<User> findAll(Map<String, Object> params, String collectionName) {
		List<User> result = mongoTemplate.find(new Query(Criteria.where("age").lt(params.get("maxAge"))), User.class,
				collectionName);
		return result;
	}

	@Override
	public void update(Map<String, Object> params, String collectionName) {
		mongoTemplate.upsert(new Query(Criteria.where("id").is(params.get("id"))),
				new Update().set("name", params.get("name")), User.class, collectionName);
	}

	@Override
	public void createCollection(String name) {
		mongoTemplate.createCollection(name);
	}

	@Override
	public void remove(Map<String, Object> params, String collectionName) {
		mongoTemplate.remove(new Query(Criteria.where("id").is(params.get("id"))), User.class, collectionName);
	}
}